<?php

use Biz\model\Album\AlbumListModel;
use Doraemon\model\ForbiddenWord\ForbiddenSceneWord as ForbiddenSceneWordModel;
use Doraemon\service\ForbiddenWord\ForbiddenSceneWord;
use Doraemon\tools\ImgUrl;

class EditAlbumInfoAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    /**
     * @throws \Exception
     */
    public function main()
    {
        $currUid = $this->uid;
        $albumId = trim($this->getRequest()->getPost('album_id', ''));
        $title = trim($this->getRequest()->getPost('title', ''));
        $coverImg = trim($this->getRequest()->getPost('cover_img', ''));
        $desc = trim($this->getRequest()->getPost('desc', ''));

        if (empty($albumId)) {
            throw new Exception('album_id参数缺失');
        }
        $albumInfo = AlbumListModel::getByWhere([
            'album_id' => $albumId,
            'status[!]' => AlbumListModel::STATUS_DELETE,
        ]);
        if (empty($albumInfo)) {
            throw new Exception('清单不存在');
        }
        if ($albumInfo['uid'] != $currUid) {
            throw new Exception('仅可以编辑自己的清单');
        }

        //敏感词过滤
        $text = $title.','.$desc;

        $remarkForbidden = new ForbiddenSceneWord(ForbiddenSceneWordModel::CONTENT, $text);
        if ($remarkForbidden->getHit()) {
            if($remarkForbidden->getPublish()){ //0不允许发布 1允许发布
                //敏感词场景处理方式 可以发布 这里不做任何处理
            }else{
                throw new Exception('嘻嘻，别闹啦~删掉敏感信息再发布吧');
            }
        }

        if(!empty($title)){
            $albumData['title'] = $title;
        }
        if(!empty($desc)){
            $albumData['desc'] = $desc;
        }
        if(!empty($coverImg) && strpos($coverImg,'http') === false){
            $albumData['cover_img'] = ImgUrl::getUrlNoWater(ImgUrl::APPTYPE_APP, $coverImg, ImgUrl::IMAGE_SIZE_ORI_NPHONE);
        }
        //用户发布完清单 且审核通过、或是待复审 再次编辑 则为待复审状态 否则为待审核状态
        if(AlbumListModel::STATUS_SUCCESS == $albumInfo['status'] || AlbumListModel::STATUS_WAIT_CHECK == $albumInfo['status']) {
            $albumData['status'] = AlbumListModel::STATUS_WAIT_CHECK;
            //待复审状态 要把分数和质量恢复到默认值
//            $albumData['score'] = 0;
//            $albumData['quality'] = 0;
        }elseif (AlbumListModel::STATUS_USER_PRIVATE == $albumInfo['status']){ //用户设置为私密状态时  不更改状态
            //不做处理
        } else {
            $albumData['status'] = AlbumListModel::STATUS_UNTREATED;
        }

        AlbumListModel::updateDataByWhere($albumData, ['album_id' => $albumInfo, 'uid' => $currUid]);
        $this->setSuccess(null, self::API_CODE_SUCCES);


    }
}